package com.android.openapp.ui;

import com.android.openapp.R;
import com.android.openapp.data.Place;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class ShowInfoDialog extends Dialog {

	Context mContext;
	int listIndex;
	Button mapButton;
	DialogInfoButtonListener mMapButtonListener;
	private boolean isBookmark;
	
	public ShowInfoDialog(Context context, int screenId, int buttonTextId, int placeListIndex) {
		super(context,android.R.style.Theme_Dialog);
		mContext = context;
		listIndex = placeListIndex;
		
		setCancelable(true);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		//inflate the layout
		LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(screenId, null);
		
        setContentView(layout);
        
        final Place place = Place.lista.get(listIndex);
		//setTitle(place.getDenominazione());
        TextView denominazione = (TextView) layout.findViewById(R.id.denominazione);
        denominazione.setText(place.getDenominazione());
        
		//UI elements
        TextView tipo = (TextView)layout.findViewById(R.id.tipo);
        TextView indirizzo = (TextView)layout.findViewById(R.id.indirizzo);
        tipo.setText(place.getTipologia());
        indirizzo.setText(place.getAddress());
        TextView contatti = (TextView) layout.findViewById(R.id.contatti);
        String cInfo = place.getContactInfo();
        if(cInfo != null)
        	contatti.setText(cInfo);
        
		//coordinate
		final Double lat = place.getLatitude();
		final Double lon = place.getLongitude();
		
		//bottone per passare alla mappa
        mapButton = (Button) layout.findViewById(android.R.id.button1);
        //mapButton.setBackgroundResource(R.drawable.on_map);
        mapButton.setOnClickListener(new View.OnClickListener(){

			@Override
			public void onClick(View v) {
				if(lat != null && lon != null){
					mMapButtonListener.onButtonClicked(listIndex, lat, lon);
					dismiss();
				}
				else {
					String text = mContext.getResources().getString(R.string.noaddressfound);
					Utils.showToast(mContext, text);
				}
			}
        	
        });
        
        //bottone share
        Button mailButton = (Button) layout.findViewById(android.R.id.button2);
        mailButton.setBackgroundResource(R.drawable.button_dialog_mail);
        mailButton.setOnClickListener(new View.OnClickListener(){
        	
			@Override
			public void onClick(View v) {
				Intent i = new Intent(Intent.ACTION_SEND);
				i.setType("text/plain");
				
				i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"recipient@example.com"});
				i.putExtra(Intent.EXTRA_SUBJECT, place.getDenominazione());
				i.putExtra(Intent.EXTRA_TEXT   , place.getDenominazione()+"\n "+place.getAddress());
				try {
				    mContext.startActivity(Intent.createChooser(i, "Share..."));
				} catch (android.content.ActivityNotFoundException ex) {
				    Toast.makeText(mContext, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
				}

			}
        	
        });
        
        //bottone preferiti
        Button bookButton = (Button) layout.findViewById(android.R.id.button3);
        /*if(place.bookmark!=1){
        bookButton.setText("Aggiungi ai preferiti");
        bookButton.setOnClickListener(new View.OnClickListener(){

			@Override
			public void onClick(View v) {
				((MainActivity)mContext).mBinder.insertBookmark(listIndex);
				Log.d("bookmark","bookmark inserito");
				
			}
        
       });
        }
        else{
        	bookButton.setText("Rimuovi dai preferiti");
            bookButton.setOnClickListener(new View.OnClickListener(){

    			@Override
    			public void onClick(View v) {
    				((MainActivity)mContext).mBinder.deleteBookmark(listIndex);
    				Log.d("bookmark","bookmark inserito");
    				
    			}
            
           });
        }
        */
        //Integer actions=place.bookmark;
        if(place.bookmark!=1){
        	bookButton.setBackgroundResource(R.drawable.bookmark_off);
        	isBookmark = false;
        }
        else{
        	bookButton.setBackgroundResource(R.drawable.bookmark_on);
        	isBookmark = true;
        }
        
        bookButton.setOnClickListener(new View.OnClickListener(){

			@Override
			public void onClick(View v) {
				switch(place.bookmark){
				
				case 0:
					place.bookmark=1;
					//((Button)v).setText("Rimuovi dai preferiti");
					((Button)v).setBackgroundResource(R.drawable.bookmark_on);
				break;
				case 1:
					//((MainActivity)mContext).mBinder.deleteBookmark(place);
					
					place.bookmark=0;
					((Button)v).setBackgroundResource(R.drawable.bookmark_off);
					break;
				}
//				Intent i = new Intent(MainActivity.BOOKMARKS_LIST_UPDATED);
//				mContext.sendBroadcast(i);
			}
        
       });
        setOnDismissListener(new OnDismissListener() {
			
			@Override
			public void onDismiss(DialogInterface dialog) {
				if (isBookmark){ // se era un bookmark ed e' stato deselezionato, lo cancello dai bookmark
					if(place.bookmark == 0){
						((MainActivity)mContext).mBinder.deleteBookmark(place);
						Log.d("bookmark","bookmark cancellato");
					}
				}
				else{ // se non era un bookmark ed e' stato selezionato, lo inserisco in lista bookmark
					if (place.bookmark == 1){
						((MainActivity)mContext).mBinder.insertBookmark(place);
						Log.d("bookmark","bookmark inserito");
					}
				}
				Intent i = new Intent(MainActivity.BOOKMARKS_LIST_UPDATED);
				mContext.sendBroadcast(i);
			}
		});
	}
	
	/**
	 * 
	 * @param listener Listener personalizzabile per definire il comportamento del bottone
	 */
	public void setMapButtonListener(DialogInfoButtonListener listener){
		mMapButtonListener = listener;
	}
	
	/**
	 * 
	 * @param resId l'icona da visualizzare in basso a destra (mappa)
	 */
	public void setMapIconResource(int resId){
		mapButton.setBackgroundResource(resId);
	}
	
}
